<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
        <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='库房'>
              <a-select v-model='queryParam.storehouse' placeholder='请选择冷库'>
                <a-select-option v-for='d in hoseList' :key='d.id' :value='d.code'>{{ d.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!--    <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='成本中心'>
                <j-dict-select-tag v-model='queryParam.cost' :triggerChange='true' dictCode='ch_cost'
                                   placeholder='请选择成本中心'>
                </j-dict-select-tag>
              </a-form-item>
            </a-col>-->
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='日期'>
              <a-range-picker :style="{width: '100%'}" format='YYYY-MM-DD'
                              :placeholder="['开始时间', '结束时间']"
                              v-model='rangepikerDate'
                              @change='onBirthdayChange' />
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="是否包含预出库">
              <a-select v-model='queryParam.isPre' placeholder="是否包含预出库"  >
                <a-select-option v-for="(d,i) in preList"  :key="d.id" :value="d.id">{{ d.value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>



          <a-col :xl='4' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchData' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button type='primary' icon='download' @click="handleExportXls('成品花出库日报表')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        aria-multiselectable='false'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'

        class='j-table-force-nowrap'
        @change='PaginationChange'
        >

        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='uploadFile(text)'>
            下载
          </a-button>
        </template>

      </a-table>
    </div>

  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'

import { mixinDevice } from '@/utils/mixin'
import JDate from '@/components/jeecg/JDate.vue'
import moment from 'moment'
import {getAction, downFile } from '@/api/manage'


export default {
  name: 'FlowerMaterialBookList',
  mixins: [  mixinDevice],
  components: {
    JDate

  },
  data() {
    return {
      description: '成本中心',
      hoseList: [], //冷库
      queryParam: {},
      preList:[{id:'',value:"全部"},{id:'1',value:"直接出库"},{id:'2',value:"预出库"}],
      rangepikerDate: [],
      /* 数据源 */
      dataSource:[],
      loading:false,
      /* 分页参数 */
      ipagination:{
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },{
          title: '基地',
          align: 'center',
          width: 100,
          dataIndex: 'sourceBase_dictText'
        }, {
          title: '大棚',
          align: 'center',
          width: 100,
          dataIndex: 'sourceGreenhouse_dictText'
        }, {
          title: '库房',
          align: 'center',
          width: 100,
          dataIndex: 'storehouse_dictText'
        }, {
          title: '成本中心',
          align: 'center',
          width: 100,
          dataIndex: 'cost_dictText'
        }, {
          title: '单号',
          align: 'center',
          dataIndex: 'orderNo'
        },  {
          title: '客户名称',
          align: 'center',
          dataIndex: 'user'
        },{
          title: '业务员',
          align: 'center',
          dataIndex: 'businessPerson'
        },
        {
          title: '出库时间',
          align: 'center',
          dataIndex: 'stockInTime'
        },
        {
          title: '出库人',
          align: 'center',
          dataIndex: 'stockOutUser'
        }, {
          title: '来源',
          align: 'center',
          dataIndex: 'source'
        }, {
          title: '账期号',
          align: 'center',
          dataIndex: 'period'
        },
        {
          title: '合计数量',
          align: 'center',
          dataIndex: 'number'
        },
        // {
        //   title: '合计总价',
        //   align: 'center',
        //   dataIndex: 'totalprice'
        // },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'state_dictText'
        },{
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },{
          title: '备注',
          align: 'center',
          dataIndex: 'remarks'
        }
      ],
      url: {
        list: '/flower/flowerCost/queryOutDayListByDate',
        exportXlsUrl: '/flower/flowerCost/queryOutDayListByDateExcel'
      },
      dictOptions: {}
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    let now = new Date()
    let monthNum = moment().month()
    // this.queryParam.sDate = moment(now).month(monthNum).date(1).startOf('month').format('YYYY-MM-DD')
    // this.queryParam.eDate = moment(now).month(monthNum).date(1).endOf('month').format('YYYY-MM-DD')
    this.queryParam.sDate = moment(now).month(monthNum).date(now.getDate()).format('YYYY-MM-DD')
    this.queryParam.eDate = moment(now).month(monthNum).date(now.getDate()).format('YYYY-MM-DD')
    this.rangepikerDate = [this.queryParam.sDate, this.queryParam.eDate]
    this.hoseList = JSON.parse(localStorage.getItem('ch_storehouse') ? localStorage.getItem('ch_storehouse') : '[]')
    this.getdata()
  },
  methods: {
    moment,
    initDictConfig() {
    },

    onBirthdayChange(date, dateString) {
      this.queryParam.sDate = dateString[0]
      this.queryParam.eDate = dateString[1]
    },
    searchData(){
      this.ipagination.current = 1
      this.getdata()
    },
    getdata(){
      var params =this.queryParam;//查询条件
        params.pageNo = this.ipagination.current
        params.pageSize = this.ipagination.pageSize

      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records||res.result;
          if(res.result.total)
          {
            this.ipagination.total = res.result.total;
          }else{
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false;
      })
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = '导出文件'
      }
      let param = this.queryParam
      fileName = fileName + '(' + this.queryParam.sDate + '-' + this.queryParam.eDate + ')'
      // console.log("导出参数",param)
      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    searchReset(){
      this.queryParam = {}
      let now = new Date()
      let monthNum = moment().month()
      this.queryParam.sDate = moment(now).month(monthNum).date(1).startOf('month').format('YYYY-MM-DD')
      this.queryParam.eDate = moment(now).month(monthNum).date(1).endOf('month').format('YYYY-MM-DD')
      this.rangepikerDate = [this.queryParam.sDate, this.queryParam.eDate]

      this.ipagination.current = 1
      this.getdata()
    },
    PaginationChange(pagination) {
      this.ipagination.current = pagination.current
      this.ipagination.pageSize = pagination.pageSize
      this.getdata()
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>